/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 10, 2011 12:12:11 PM
 */
package carrello.model.entities;

import carrello.model.entities.Product;
import carrello.util.EntityManagerUtil;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.Cascade;

@NamedQueries({
	@NamedQuery(name = "getAllCategories", query = "from Category"),
        @NamedQuery(name = "getCategoryByName", query = "from Category c where c.name = :s"),
        @NamedQuery(name = "getAllParent", query = "select c.parentId from Category c"),
        @NamedQuery(name = "getAttributes", query = "select c.attribute from Category c where c.name = :sid"),
        @NamedQuery(name = "getAttributesById", query = "select c.attribute from Category c where c.category_id = :id"),
        @NamedQuery(name = "getCategoryById", query = "from Category c where c.category_id = :c"),
        @NamedQuery(name = "getID", query = "select c.category_id from Category c where c.name = :name")
        })
@Entity
@Table(name = "Category")
public class Category {


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)    
    @Column(name = "category_id")
    private int category_id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name ="parent_id", nullable = true)
    private Category parentId;
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parentId")
    private Set<Category> children;
    
    private String name;
    private String description;
    
    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, mappedBy = "category")
    private Set<Product> product;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "CategoryHasAttribute" ,
        joinColumns = { @JoinColumn(name = "category_id")}, 
        inverseJoinColumns = { @JoinColumn(name = "attribute_id")})
    private Set<Attribute> attribute;
    
    public Category() {
    }

    public Category(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public Category(Category parent, String name, String description) {
        this.parentId = parent;
        this.name = name;
        this.description = description;
    }

    public Category(Category parent, String name, String description, Set<Product> product, Set<Attribute> attribute) {
        this.parentId = parent;
        this.name = name;
        this.description = description;
        this.product = product;
        this.attribute = attribute;
    }
    
    public int getCategory_id() {
        return category_id;
    }

    public void setCategory_id(int categoryId) {
        this.category_id = categoryId;
    }

    public Set<Category> getchildren () {
        return children;
    }
    public void setchildren (Set<Category> c) {
        children = c;
    }
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public Category getParentId() {
        return parentId;
    }

    public void setParentId(Category parentId) {
        this.parentId = parentId;
    }

    public Set<Product> getProduct() {
        return product;
    }

    public void setProduct(Set<Product> product) {
    }

    public Set<Attribute> getAttribute() {
        return attribute;
    }

    public void setAttribute(Set<Attribute> attribute) {
        this.attribute = attribute;
    }
    public static List<Category> getAllCategories () {
       {
       List<Category> cats = EntityManagerUtil.getEntityManager().getQuery("getAllCategories").list();
       return cats;
       }
    }
    
    public static List<Attribute> getAttributesByCategoryId (String name) {
       {
       List<Attribute> attList = EntityManagerUtil.getEntityManager().getQuery("getAttributes") .setParameter("sid", name).list();
       return attList;
       }
    }
    
    public static List<Attribute> getAttributesById (int cid) {
       {
       List<Attribute> attList = EntityManagerUtil.getEntityManager().getQuery("getAttributesById") .setParameter("id", cid).list();
       return attList;
       }
    }
    
     public static List<Category> getAllParent() {
       {
           List<Category> cats = EntityManagerUtil.getEntityManager().getQuery("getAllParent").list();
       return cats;
       }
    }
    
      public static Category getCategoryByName(String name) {
        Category category = (Category)EntityManagerUtil.getEntityManager().getQuery("getCategoryByName")
                                    .setParameter("s", name)
                                    .uniqueResult();
        return category;
    }
    public static Category getCategoryById(int categoryid) {
        Category category = (Category)EntityManagerUtil.getEntityManager().getQuery("getCategoryById")
                                    .setParameter("c", categoryid)
                                    .uniqueResult();
        return category;
    }
    
    public static int getID(String name)
    {
            return (Integer) EntityManagerUtil.getEntityManager().getQuery("getID")
                                    .setParameter("name", name)
                                    .uniqueResult();
    }
}